var GameData = require('../module/gameData')();
var that;
cc.Class({
    extends: cc.Component,
    properties: {
        left_scroll: cc.ScrollView, /* 左边scrollview */
        overItem: cc.Prefab,
        countNum: cc.Label,
        right_pro: cc.ProgressBar, /* 右边进度条 */
        right_title: cc.Label, /* 右边title */
        is_rotation: false,
        goal_pro: null, /* 完成的某个场景的 进度*/
        _progress: 0,
        jintu: cc.Label,
        down: cc.Node,
        up: cc.Node,
        right: cc.Node,
    },
    // LIFE-CYCLE CALLBACKS:
    onLoad() {
        GameData.playMusic(GameData.GameLayer.moveMusic[7]);
        this.iscan = false;
        this.up.runAction(cc.moveTo(0.3, 0, 230));
        var that = this;
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouch, this);
        this.left_scroll.node.runAction(
            cc.sequence(
                cc.moveTo(0.3, 0, -57),
                cc.moveTo(0.2, -152, -57),
                cc.callFunc(function () {
                    that.right.opacity = 0;
                    that.right.active = true;
                    that.init();
                    that.right.runAction(
                        cc.fadeIn(0.2)
                    )
                }),
                cc.delayTime(0.2),
                cc.callFunc(function () {
                    that.down.opacity = 0;
                    that.down.active = true;
                    that.down.runAction(
                        cc.fadeIn(0.2)
                    )
                }),
                cc.delayTime(0.2),
                cc.callFunc(function () {
                    that.iscan = true;

                })
            ));
    },
    init() {
        this.is_rotation = false;
        that = this;
        this._process = 0;
        this.fishData = GameData.getlocalData("fishData");
        this._process = this.fishData.handbook.oldfish.length / GameData.GameLayer.fishprefab.length;
        this.right_pro.progress = this._process;

        this.content = this.left_scroll.content;
        that.over_pool = new cc.NodePool();
        this.populateList();
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouch, this);
        // this.right_pro.progress = 0.5;

        /*在本局结束时  完成对应场景的 进度  通过计算获得的 */

    },
    onTouch(evt) {
        if (this.iscan) {
            cc.find("gameOver").active = false;
            cc.director.loadScene("Home");
        }
    },
    populateList() {
        let _countNum = 0;
        for (var i = 0; i < GameData.overData.length; ++i) {
            let obj = GameData.overData[i];
            if (obj.id&&obj.id > this.fishData.unlockFish && GameData.puTongyuId.indexOf(obj.id) != -1) {
                this.fishData.unlockFish = obj.id;
            }
            if (typeof obj.id =="number"&&this.fishData.handbook.oldfish.indexOf(obj.id) == -1) {
                this.fishData.handbook.newfish.push(obj.id);
                console.log("打到的新鱼",obj.id)
            }
            var item;
            if (that.over_pool.size() > 0) {
                item = that.over_pool.get();
            } else {
                item = cc.instantiate(this.overItem);
            }
            _countNum += obj.price;
            item.getComponent('overItem').init(obj);
            this.content.addChild(item);
        }
        this.countNum.string = _countNum + "";
        GameData.GameLayer.coinNum.node.parent.getComponent("gamecoin").coinAddEffect(this.fishData.coinNum, this.fishData.coinNum + _countNum);
        this.fishData.coinNum += _countNum;
        GameData.savelocalData(this.fishData);
        let _fishData = GameData.getlocalData("fishData");
        this.goal_pro = (_fishData.handbook.oldfish.length + _fishData.handbook.newfish.length) / GameData.GameLayer.fishprefab.length;
        this.jintu.string = parseInt(this.goal_pro * 100) + "%";
        setTimeout(function () {
            that.is_rotation = true;
        }, 200);
    },
    start() {

    },
    /*
    *description: 继续游戏 按钮
    *author: <zhouzhiwei>
    *@param:
    *date:
    */

    continue_fish() {
        cc.find("gameOver").active = false;
        cc.director.loadScene("Home");
    },
    lateUpdate(dt) {
        if (this.is_rotation) {
            this._process += dt;
            this.right_pro.progress = this._process;
            if (this._process >= this.goal_pro) {
                this._process = this.goal_pro;
                this.is_rotation = false;
            }
        }
    },
});
